<?php
namespace App\Controllers\Sys;

use App\Controllers\BaseController;
use App\Models\Receipt;
use App\Library\Helper;

class ReceiptController extends BaseController {

    public function initialize() {
        parent::initialize();
        if($this->request->isPost()) {
            $this->checkLogin();
        }
    }

    //---------------------------------------------------------------------------------
    //特别互动列表
    //---------------------------------------------------------------------------------
    public function indexAction() {
        $this->display('/sys/receipt/index');
    }
    //---------------------------------------------------------------------------------
    //统计特别互动的总数
    //---------------------------------------------------------------------------------
    public function countAction() {
        $log = new Receipt();
        $ret = $log->total($this->parseFilter());
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取总数失败，系统错误！");
        }
    }

    //---------------------------------------------------------------------------------
    //获取特别互动内容
    //---------------------------------------------------------------------------------
    public function getsAction() {
        $data = [
            'page' => $this->request->getPost('page'),
            'size' => $this->request->getPost('size'),
        ];

        if($data['page'] == '' || !is_numeric($data['page'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前的页数！');
        }
        if($data['size'] == '' || !is_numeric($data['size'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前指定每页条数！');
        }
        $data['page'] = intval($data['page']);
        $data['size'] = intval($data['size']);

        $offset = ($data['page'] - 1) * $data['size'];
        $log    = new Receipt();
        $ret    = $log->gets($this->parseFilter(), $offset, $data['size']);
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取数据失败，系统错误！");
        }
    }

    //---------------------------------------------------------------------------------
    //查看具体的信息
    //---------------------------------------------------------------------------------
    public function detailAction() {
        if($this->request->isGet()) {
            $this->display('/sys/receipt/detail');
        } else {
            $id = $this->request->getPost('id') != '' ? $this->request->getPost('id') : $this->request->getQuery('id');
            if($id != '' && is_numeric($id)) {
                $log                 = new Receipt();
                $entity              = $log->get($id);
                $entity['mt_status'] = '发送成功';
                return $this->sendAjax(true, $entity);
            } else {
                return $this->sendAjax(false, '获取数据失败，参数不正确！');
            }
        }
    }



    //---------------------------------------------------------------------------------
    //搜索特别互动条件筛选
    //---------------------------------------------------------------------------------
    private function parseFilter() {
        $where = $this->request->getPost('filter');
        if($where['mobile'] != '') {
            $where['mobile'] = $where['mobile'];
        } else {
            unset($where['mobile']);
        }
        if($where['status'] != '') {
            $where['status'] = $where['status'];
        } else {
            unset($where['status']);
        }
        if($where['receive_at'] != '') {
            $where['receive_at'] = Helper::getDatepickerDateToStandardDate($where['receive_at']);
        } else {
            unset($where['receive_at']);
        }
        if($where['create_at'] != '') {
            $where['create_at'] = Helper::getDatepickerDateToStandardDate($where['create_at']);
        } else {
            unset($where['create_at']);
        }
        return $where;
    }

}
